﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class NormList : PowerBase
    {
        CJJCH.BLL.T_INF_Norm bll_norm = new CJJCH.BLL.T_INF_Norm();
        CJJCH.BLL.T_INF_NormType bll_type = new BLL.T_INF_NormType();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.form1.DefaultButton = "btnSearch";
            if (!IsPostBack)
            {
                InitControl();
                InitRequest();
            }
        }

        protected void InitControl()
        {
            IList<CJJCH.Model.T_INF_NormType> list = new List<CJJCH.Model.T_INF_NormType>();
            list = bll_type.GetTypeList("");
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    dropNormType.Items.Add(new ListItem(list[i].TypeName, list[i].ID.ToString()));
                }
                list = null;
            }
        }

        protected void InitRequest()
        {
            if (Request.QueryString["keyword"] != null)
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
                txtKeyword.Text = lblKeyword.Text;
            }
            if (Request.QueryString["orderkey"] != null)
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());
            }
            if (Request.QueryString["ordertype"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["ordertype"].Trim()))
            {
                lblOrderType.Text = Request.QueryString["ordertype"].Trim();
            }
            if (Request.QueryString["keytype"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["keytype"].Trim()))
            {
                lblKeyType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keytype"].Trim());
                dropNormType.SelectedValue = lblKeyType.Text.Trim();
            }
            else
            {
                lblKeyType.Text = "0";
            }
            if (Request.QueryString["betime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["betime"].Trim()))
            {
                lblBeTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
                txtBegin.Text = lblBeTime.Text;
            }
            if (Request.QueryString["edtime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["edtime"].Trim()))
            {
                lblEdTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
                txtEnd.Text = lblEdTime.Text;
            }
            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].ToString()))
            {
                int curPage = int.Parse(Request.QueryString["curpage"].ToString());
                AspNetPager1.RecordCount = curPage * AspNetPager1.PageSize;
                AspNetPager1.CurrentPageIndex = curPage;
            }
            else
            {
                AspNetPager1.CurrentPageIndex = 1;
            }
        }

        protected void ReapterDataBinding()
        {
            string keyword = LTP.Common.StringClass.EncodeString(lblKeyword.Text.Trim());
            string beginTime = "";
            try
            {
                beginTime = Convert.ToDateTime(lblBeTime.Text.Trim()).ToString();
            }
            catch { }
            string endTime = "";
            try
            {
                endTime = Convert.ToDateTime(lblEdTime.Text.Trim()).AddDays(1).ToString();
            }
            catch { }
            int typeID = 0;
            try
            {
                typeID = Convert.ToInt32(this.lblKeyType.Text.Trim()); 
            }
            catch { }
            string strOrder = "";
            if (lblOrderKey.Text.Trim() != "")
            {
                strOrder += lblOrderKey.Text.Trim();
                if (lblOrderType.Text.Trim() == "1")
                {
                    strOrder += " desc,";
                }
                else
                {
                    strOrder += " asc,";
                }
            }
            int totalCount = 0;
            IList<CJJCH.Model.T_INF_Norm> list = new List<CJJCH.Model.T_INF_Norm>();
            list = bll_norm.GetNormList(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, keyword, typeID, beginTime, endTime, ref totalCount, strOrder);
            AspNetPager1.RecordCount = totalCount > (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize ? totalCount : AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize;
            rptNorm.DataSource = list;
            rptNorm.DataBind();
            if (list.Count == 0)
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            else
                litMessage.Text = "";
            AspNetPager1.CustomInfoHTML = "共有：<font color=\"blue\"><b>" + totalCount.ToString() + "</b></font>条记录";
            AspNetPager1.CustomInfoHTML += " <font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
            AspNetPager1.CustomInfoHTML += "/<font color=\"blue\"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
        }

        protected void rptNorm_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((LinkButton)e.Item.FindControl("lbtnTitle")).Text = ((CJJCH.Model.T_INF_Norm)e.Item.DataItem).NormTitle;
                ((LinkButton)e.Item.FindControl("lbtnTitle")).Attributes.Add("onclick", "ShowFinish('" + ((CJJCH.Model.T_INF_Norm)e.Item.DataItem).ID.ToString() + "');return false;");
                ((Label)e.Item.FindControl("lblType")).Text = ((CJJCH.Model.T_INF_Norm)e.Item.DataItem).Normtype.TypeName;
                ((Label)e.Item.FindControl("lblPlusDate")).Text = ((CJJCH.Model.T_INF_Norm)e.Item.DataItem).PlusDate.ToString("yyyy/MM/dd");
                ((Label)e.Item.FindControl("lblHits")).Text = ((CJJCH.Model.T_INF_Norm)e.Item.DataItem).Hits.ToString();
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_INF_Norm)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_INF_Norm)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定要删除该条数据吗？')){return true;}else{return false;}");
            }
        }

        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                Response.Redirect("NormEdit.aspx?ID=" + ((LinkButton)sender).CommandArgument.ToString()
                    + "&curpage=" + AspNetPager1.CurrentPageIndex.ToString()
                    +"&keyword="+LTP.Common.StringClass.EncodeUrl(this.lblKeyword.Text.Trim())
                    + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeyType.Text.Trim())
                    + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                    + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                    + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblBeTime.Text.Trim())
                    + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblEdTime.Text.Trim()));
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("错误操作！");
            }
        }

        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                string error = bll_norm.DelNorm(new Guid(((LinkButton)sender).CommandArgument));
                if (string.IsNullOrEmpty(error.Trim()))
                {
                    Response.Redirect("NormList.aspx?curpage=" + AspNetPager1.CurrentPageIndex.ToString()
                        + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&keytype=" + LTP.Common.StringClass.EncodeUrl(lblKeyType.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblBeTime.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblEdTime.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
                LTP.Common.WindowClass.WindowBack("操作错误！");
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            ReapterDataBinding();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblKeyword.Text = LTP.Common.StringClass.EncodeString(txtKeyword.Text.Trim());
            lblBeTime.Text = LTP.Common.StringClass.EncodeString(txtBegin.Text.Trim());
            lblEdTime.Text = LTP.Common.StringClass.EncodeString(txtEnd.Text.Trim());
            lblKeyType.Text = LTP.Common.StringClass.EncodeString(dropNormType.SelectedValue);
            AspNetPager1.CurrentPageIndex = 1;
        }

        #region 抬头排序功能

        protected void lbtnTitle_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = " N.NormTitle ";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lbtnType_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = " N.TypeID ";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lbtnHits_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = " N.Hits ";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }
        #endregion
    }
}